﻿namespace Smart.Utils.System2
{
    using Microsoft.Win32;
    using System;

    public class RegistryHelper
    {
        public bool CreateKey(ERegistryPossibleRoots lngrootkey, string strKey)
        {
            RegistryKey regKey = null;
            bool flag;
            try
            {
                regKey = this.GetRegKey(lngrootkey);
                regKey = regKey.CreateSubKey(strKey);
                if (regKey == null)
                {
                    return false;
                }
                flag = true;
            }
            catch (Exception exception)
            {
                throw new Exception("Fehler in CreateKey!", exception);
            }
            finally
            {
                try
                {
                    regKey.Close();
                }
                catch
                {
                }
                regKey = null;
            }
            return flag;
        }

        public bool CreateValue(ERegistryPossibleRoots lngrootkey, string strKey, string strValName, int objVal)
        {
            RegistryKey regKey = null;
            bool flag;
            try
            {
                regKey = this.GetRegKey(lngrootkey);
                regKey = regKey.OpenSubKey(strKey, true);
                if (regKey != null)
                {
                    regKey.SetValue(strValName, objVal);
                    regKey.Flush();
                    return true;
                }
                flag = false;
            }
            catch (Exception exception)
            {
                throw new Exception("Fehler in CreateValue(string)!", exception);
            }
            finally
            {
                try
                {
                    regKey.Close();
                }
                catch
                {
                }
                regKey = null;
            }
            return flag;
        }

        public bool CreateValue(ERegistryPossibleRoots lngrootkey, string strKey, string strValName, byte[] objVal)
        {
            RegistryKey regKey = null;
            bool flag;
            try
            {
                regKey = this.GetRegKey(lngrootkey);
                regKey = regKey.OpenSubKey(strKey, true);
                if (regKey != null)
                {
                    regKey.SetValue(strValName, objVal);
                    regKey.Flush();
                    return true;
                }
                flag = false;
            }
            catch (Exception exception)
            {
                throw new Exception("Fehler in CreateValue(byte[])!", exception);
            }
            finally
            {
                try
                {
                    regKey.Close();
                }
                catch
                {
                }
                regKey = null;
            }
            return flag;
        }

        public bool CreateValue(ERegistryPossibleRoots lngrootkey, string strKey, string strValName, string objVal)
        {
            RegistryKey regKey = null;
            bool flag;
            try
            {
                regKey = this.GetRegKey(lngrootkey);
                regKey = regKey.OpenSubKey(strKey, true);
                if (regKey != null)
                {
                    regKey.SetValue(strValName, objVal);
                    regKey.Flush();
                    return true;
                }
                flag = false;
            }
            catch (Exception exception)
            {
                throw new Exception("Fehler in CreateValue(string)!", exception);
            }
            finally
            {
                try
                {
                    regKey.Close();
                }
                catch
                {
                }
                regKey = null;
            }
            return flag;
        }

        public bool DeleteKey(ERegistryPossibleRoots lngrootkey, string strKey, params bool[] bRecursive)
        {
            RegistryKey regKey = null;
            bool flag;
            try
            {
                regKey = this.GetRegKey(lngrootkey);
                if (bRecursive.Length > 0)
                {
                    if (bRecursive[0])
                    {
                        regKey.DeleteSubKeyTree(strKey);
                    }
                    else
                    {
                        regKey.DeleteSubKey(strKey);
                    }
                }
                else
                {
                    regKey.DeleteSubKey(strKey);
                }
                flag = true;
            }
            catch (Exception exception)
            {
                throw new Exception("Fehler in DeleteKey", exception);
            }
            finally
            {
                if (regKey != null)
                {
                    try
                    {
                        regKey.Close();
                    }
                    catch
                    {
                    }
                }
                regKey = null;
            }
            return flag;
        }

        public bool DeleteValue(ERegistryPossibleRoots lngrootkey, string strKey, string strValName)
        {
            RegistryKey regKey = null;
            bool flag;
            try
            {
                regKey = this.GetRegKey(lngrootkey);
                regKey = regKey.OpenSubKey(strKey, true);
                if (regKey != null)
                {
                    regKey.DeleteValue(strValName);
                    regKey.Flush();
                    return true;
                }
                flag = false;
            }
            catch (Exception exception)
            {
                throw new Exception("Fehler in DeleteValue!", exception);
            }
            finally
            {
                try
                {
                    regKey.Close();
                }
                catch
                {
                }
                regKey = null;
            }
            return flag;
        }

        public bool DoesKeyExist(ERegistryPossibleRoots lngRootKey, string strKey)
        {
            RegistryKey key = null;
            bool flag;
            try
            {
                key = this.GetRegKey(lngRootKey).OpenSubKey(strKey, false);
                if (key == null)
                {
                    return false;
                }
                key.Close();
                key = null;
                flag = true;
            }
            catch (Exception exception)
            {
                throw new Exception("Fehler in DoesKeyExist!", exception);
            }
            return flag;
        }

        private RegistryKey GetRegKey(ERegistryPossibleRoots lngRoot)
        {
            RegistryKey key;
            try
            {
                switch (lngRoot)
                {
                    case ERegistryPossibleRoots.HKEY_CLASSES_ROOT:
                        return Registry.ClassesRoot;

                    case ERegistryPossibleRoots.HKEY_CURRENT_CONFIG:
                        return Registry.CurrentConfig;

                    case ERegistryPossibleRoots.HKEY_CURRENT_USER:
                        return Registry.CurrentUser;

                    case ERegistryPossibleRoots.HKEY_DYNDATA:
                        return Registry.DynData;

                    case ERegistryPossibleRoots.HKEY_LOCALE_MACHINE:
                        return Registry.LocalMachine;

                    case ERegistryPossibleRoots.HKEY_PERFORMANCE_DATA:
                        return Registry.PerformanceData;

                    case ERegistryPossibleRoots.HKEY_USERS:
                        return Registry.Users;
                }
                key = null;
            }
            catch (Exception exception)
            {
                throw new Exception("Fehler in GetRegKey!", exception);
            }
            return key;
        }

        public int QueryValue(ERegistryPossibleRoots lngrootkey, string strKey, string strValName, int objDefault)
        {
            RegistryKey regKey = null;
            int num;
            try
            {
                regKey = this.GetRegKey(lngrootkey);
                regKey = regKey.OpenSubKey(strKey);
                num = (int) regKey.GetValue(strValName, objDefault);
            }
            catch (Exception exception)
            {
                throw new Exception("Fehler in QueryValue(int)!", exception);
            }
            finally
            {
                if (regKey != null)
                {
                    try
                    {
                        regKey.Close();
                    }
                    catch
                    {
                    }
                }
                regKey = null;
            }
            return num;
        }

        public string QueryValue(ERegistryPossibleRoots lngrootkey, string strKey, string strValName, string objDefault)
        {
            RegistryKey regKey = null;
            string str;
            try
            {
                regKey = this.GetRegKey(lngrootkey);
                regKey = regKey.OpenSubKey(strKey);
                str = (string) regKey.GetValue(strValName, objDefault);
            }
            catch (Exception exception)
            {
                throw new Exception("Fehler in QueryValue(string)!", exception);
            }
            finally
            {
                if (regKey != null)
                {
                    try
                    {
                        regKey.Close();
                    }
                    catch
                    {
                    }
                }
                regKey = null;
            }
            return str;
        }

        public byte[] QueryValue(ERegistryPossibleRoots lngrootkey, string strKey, string strValName, byte[] objDefault)
        {
            RegistryKey regKey = null;
            byte[] buffer;
            try
            {
                regKey = this.GetRegKey(lngrootkey);
                regKey = regKey.OpenSubKey(strKey);
                buffer = (byte[]) regKey.GetValue(strValName, objDefault);
            }
            catch (Exception exception)
            {
                throw new Exception("Fehler in QueryValue(byte[])!", exception);
            }
            finally
            {
                if (regKey != null)
                {
                    try
                    {
                        regKey.Close();
                    }
                    catch
                    {
                    }
                }
                regKey = null;
            }
            return buffer;
        }
    }
}

